<?php
namespace Admin\Controller;
class HouseController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('House');
        $this->_cate_mod = D('HouseCate');
    }

   /* public function _before_index() {
        $this->list_relation = true;
        $p = I('p',1,'intval');
        $this->_data = array(
            'addDynamic'    => array(
                'c' => 'Article',
                'a' => 'add',
                'str'    => '<a href="%s">添加资讯</a>',
                'param' => array('house_id'=>'@id@','menuid'=>23),
                'isajax' => 0,
                'replace'=> ''
            ),
            'addPicture'    => array(
                'c' => 'HousePicture',
                'a' => 'add',
                'str'    => '<a href="%s">上传图片</a>',
                'param' => array('house_id'=>'@id@','menuid'=>285),
                'isajax' => 0,
                'replace'=> ''
            ),
            'addHouseType'    => array(
                'c' => 'HouseType',
                'a' => 'add',
                'str'    => '<a href="%s">添加户型</a>',
                'param' => array('house_id'=>'@id@','menuid'=>218),
                'isajax' => 0,
                'replace'=> ''
            ),
            'houseTypeIndex'    => array(
                'c' => 'HouseType',
                'a' => 'index',
                'str'    => '<a href="%s">户型管理</a>',
                'param' => array('house_id'=>'@id@','menuid'=>218),
                'isajax' => 0,
                'replace'=> ''
            ),
            'housePrice'    => array(
            'c' => 'HousePrice',
            'a' => 'index',
            'str'    => '<a href="%s">价格</a>',
            'param' => array('house_id'=>'@id@'),
            'isajax' => 0,
            'replace'=> ''
            ),
            'property'    => array(
                'c' => 'Property',
                'a' => 'index',
                'str'    => '<a href="%s">沙盘</a>',
                'param' => array('house_id'=>'@id@'),
                'isajax' => 0,
                'replace'=> ''
            ),
        );

        $this->assign('p',$p);
    }*/

   /* protected function _search() {
        $map = array();
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $area_id = I('get.area_id',0,'int');
		if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($area_id) {
            $id_arr = $this->_cate_mod->get_child_ids($area_id, true);
            $map['area_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$area_id))->getField('spid');
            $selected_ids = $spid ? $spid . $area_id : $area_id;
        }

        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'area_id' => $area_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }*/

    public function _before_add(){
       // $this->developers();
    }

    protected function _before_insert($data) {
        $data['house_type'] = !empty($data['house_type']) ? '|'.implode('|',$data['house_type']).'|' : '';
        $data['tags'] = !empty($data['tags']) ? '|'.implode('|',$data['tags']).'|' : '';
        $data['floor'] = !empty($data['floor']) ? '|'.implode('|',$data['floor']).'|' : '';
        $data['room_type'] = !empty($data['room_type']) ? '|'.implode('|',$data['room_type']).'|' : '';
        $data['address'] = implode('|',$data['address']);
        $data['tel'] = implode('|',$data['tel']);
        $data['open_time']   = (empty($data['open_time'])) ? 0 : strtotime($data['open_time']);
        $data['room_time']   = (empty($data['room_time'])) ? 0 : strtotime($data['room_time']);
        $data['update_time'] = time();
        return $this->_insertPic($data);
    }

    protected function _after_insert($id){
        $d = array();
        $d['house_id'] = $_POST['houseInfo']['house_id'] = $id;
        M('house_info')->add($_POST['houseInfo']);
        $d['mating'] = serialize($_POST['mating']);
        $d['data'] = serialize($_POST['data']);
        $d['info'] = $_POST['info'];
        $d['special'] = $_POST['special'];
        $d['shortcoming'] = $_POST['shortcoming'];
        $d['price_analysis'] = $_POST['price_analysis'];
        M('house_data')->where(array('house_id'=>$id))->add($d);
    }

    public function _before_edit(){
        $id = I('get.id');
        $house = $this->_mod->where(array('id'=>$id))->find();
        $house_info = M('house_info')->where(array('house_id'=>$id))->find();
        $house_data = M('house_data')->where(array('house_id'=>$id))->find();
        $spid = $this->_cate_mod->where(array('id'=>$house['area_id']))->getField('spid');
        if( $spid==0 ){
            $spid = $house['area_id'];
        }else{
            $spid .= $house['area_id'];
        }
        $this->assign('house_type',explode('|',trim($house['house_type'])));
        $this->assign('room_type',explode('|',trim($house['room_type'])));
        $this->assign('address',explode('|',trim($house['address'])));
        $this->assign('tel',explode('|',trim($house['tel'])));
        $this->assign('tags',explode('|',trim($house['tags'])));
        $this->assign('floor',explode('|',trim($house['floor'])));
        $this->assign('mating',unserialize($house_data['mating']));
        $this->assign('data',unserialize($house_data['data']));
        $this->assign('house_info',$house_info);
        $this->assign('house_data',$house_data);
        $this->assign('selected_ids',$spid);
     //   $this->developers();
    }

    protected function _before_update($data) {
        $data['house_type'] = !empty($data['house_type']) ? '|'.implode('|',$data['house_type']).'|' : '';
        $data['tags'] = !empty($data['tags']) ? '|'.implode('|',$data['tags']).'|' : '';
        $data['floor'] = !empty($data['floor']) ? '|'.implode('|',$data['floor']).'|' : '';
        $data['room_type'] = !empty($data['room_type']) ? '|'.implode('|',$data['room_type']).'|' : '';
        $data['address'] = implode('|',$data['address']);
        $data['tel'] = implode('|',$data['tel']);
        $data['open_time']   = (empty($data['open_time'])) ? 0 : strtotime($data['open_time']);
        $data['room_time']   = (empty($data['room_time'])) ? 0 : strtotime($data['room_time']);
        $data['update_time'] = time();
        $new_data = $this->_updatePic($data);
        $info = M('house')->where(array('id'=>$new_data['id']))->find();
        if($info['is_index'] == 1 && $new_data['img'] && $_FILES['img']['name']){
            $info['img'] = $new_data['img'];
            D('Position')-> where(array('hid'=>$info['id'],'model'=>'house'))->save(array('data'=>serialize($info)));
        }
        return $new_data;
    }

    protected function _after_update($id){
        M('house_info')->where(array('house_id'=>$id))->save($_POST['houseInfo']);
        $d = array();
        $d['mating'] = serialize($_POST['mating']);
        $d['data'] = serialize($_POST['data']);
        $d['info'] = $_POST['info'];
        $d['special'] = $_POST['special'];
        $d['shortcoming'] = $_POST['shortcoming'];
        $d['price_analysis'] = $_POST['price_analysis'];
        M('house_data')->where(array('house_id'=>$id))->save($d);
    }


    public function ajaxCheckTitle(){
        $title = urldecode(I('get.J_title',''));
        if($title){
            $info = $this->_mod->where(array('title'=>$title))->find();
            if($info){
                $this->ajaxReturn(0);
            }else{
                $this->ajaxReturn(1);
            }
        }else{
            $this->ajaxReturn(1);
        }
    }

    public function publicSelectHouse(){
        $this->type = I('get.type') ? I('get.type') : 'radio';
        $model = I('get.model','house');
        $where = array('status'=>1);
        $obj = D($model);
        $this->result = $obj->where($where)->order('ordid desc,id desc')->field('id,title,img')->limit(24)->select();
        $count   = $obj->where($where)->count();
        $pageObj = new \Org\Util\Page($count,24);
        $this->pages=$pageObj->ajaxPage(1);
        $this->modelarr = getModel();
        $this->model = $model;
        $response = $this->fetch('selecthouse');
        $this->ajaxReturn(1,'',$response);
    }

    public function ajaxSelectHouse(){
        $pagesize = 24;
        $cate_id = I('get.area_id',0,'int');
        $q      = I('get.q');
        $page   = I('get.page',1,'int');
        $where  = array();
        if($cate_id){
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $where['area_id'] = array('IN', $id_arr);
        }
        if($q){
            $where['title'] = array('like','%'.$q.'%');
        }
        // $productObj = M('product');
        //分页类
        $count   = $this->_mod->where($where)->count();
        $pageObj = new \Org\Util\Page($count,$pagesize);
        $result = $this->_mod->where($where)->field('id,img,title')->order('ordid desc,id desc')->page($page,$pageObj->listRows)->select();
        $res    = array('page'=>$pageObj->ajaxPage($page),'data'=>$result);
        $this->ajaxReturn(1,'',$res);
    }

    public function publicPositionHouse(){
        $this->type = I('get.type') ? I('get.type') : 'radio';
        $model = I('get.model','house');
        $where = array('status'=>1);
        $obj = D($model);
        $this->result = $obj->where($where)->order('ordid desc,id desc')->field('id,title,img')->limit(24)->select();
        $count   = $obj->where($where)->count();
        $pageObj = new \Org\Util\Page($count,24);
        $this->pages=$pageObj->ajaxPage(1);
        $this->modelarr = getModel();
        $this->model = $model;
        $response = $this->fetch('positionhouse');
        $this->ajaxReturn(1,'',$response);
    }
    public function ajaxPositionHouse(){
        $page_size = 24;
        $area_id = I('get.area_id',0,'int');
        $q      = I('get.q');
        $page   = I('get.page',1,'int');
        $model  = I('get.model');
        $where  = array();
        if($area_id){
            if($model=='house'){
                $id_arr = $this->_cate_mod->get_child_ids($area_id, true);
            }else{
                $id_arr = D("area_cate")->get_child_ids($area_id, true);
            }

            $where['area_id'] = array('IN', $id_arr);
        }
        if($q){
            $where['title'] = array('like','%'.$q.'%');
        }
        $this->_mod = M($model);
        //分页类
        $count   = $this->_mod->where($where)->count();
        $pageObj = new \Org\Util\Page($count,$page_size);
        $result = $this->_mod->where($where)->field('id,img,title')->order('ordid desc,id desc')->page($page,$pageObj->listRows)->select();
        $res    = array('page'=>$pageObj->ajaxPage($page),'data'=>$result,'model'=>$model);
        $this->ajaxReturn(1,'',$res);
    }

    private function developers(){
        $developers = M('developers')->where(array('status'=>1))->order('id desc')->select();
        $this->assign('developers',$developers);
    }
}